package databases

import (
	"etcd/config"
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

var DB *gorm.DB

func initDB()  {
	conf := config.GetConfig()
	dbConfig := conf.Data.Db
	//fmt.Println(dbConfig)
	//if conn.DB == nil {
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=True&loc=%s",
		dbConfig.Username, dbConfig.Password, dbConfig.Host, dbConfig.Port, dbConfig.Dbname, dbConfig.Charset, dbConfig.TimeZone)
	var err error
	DB, err = gorm.Open(dbConfig.Driver, dsn)
	if err != nil {
		fmt.Printf("mysql connect error %v", err)
	}
	if DB.Error != nil {
		fmt.Printf("database error %v", DB.Error)
	}
	DB.SingularTable(true)
}
